#include "Common.h"
#include "World.h"
#include "Player.h"
#include "Opcodes.h"
#include "Chat.h"
#include "ObjectAccessor.h"
#include "Language.h"
#include "AccountMgr.h"
#include "SystemConfig.h"
#include "revision.h"
#include "Util.h"
#include "ScriptPCH.h"
#include <cstring>
#include "CharacterDatabaseCleaner.h"
#include "DatabaseEnv.h"
#include "ObjectMgr.h"
#include "Unit.h"
#include "Player.h"

class recovery : public CreatureScript
{
    public:

        recovery()
            : CreatureScript("recovery")
        {
        }

        void RewardItem(Player* pPlayer, Creature* pCreature, int item , int count, int cost)
        {
            QueryResult result;
            result = LoginDatabase.PQuery("SELECT recovery FROM account WHERE id = '%u'", pPlayer->GetSession()->GetAccountId());
            char str[200];
            if (!result) 
            {
                sprintf(str,"Ein Fehler ist aufgetreten. Bitte wende dich an einen Administrator!");
                pPlayer->PlayerTalkClass->ClearMenus();
                OnGossipHello(pPlayer, pCreature);
                pCreature->MonsterSay(str, LANG_UNIVERSAL, pPlayer->GetGUID());
                return;
            }
            Field *fields = result->Fetch();
            uint32 points = fields[0].GetUInt32();

            if (item == 0)
            {
                sprintf(str,"Du hast %u Recoverypoints!", points);
                pPlayer->MonsterWhisper(str,pPlayer->GetGUID(),true);
            }
            else
            {
                if (points < cost)
                {
                     sprintf(str,"Du hast nicht genug RecoveryPoints!");
                     pPlayer->MonsterWhisper(str,pPlayer->GetGUID(),true);
                }
                else
                {
                    if (pPlayer->AddItem(item, count))
                    {
                        LoginDatabase.PQuery("UPDATE account SET recovery = recovery - '%u' WHERE id = '%u'", cost, pPlayer->GetSession()->GetAccountId());
                        sprintf(str,"Deine Recovery abgeschlossen!");
                        pPlayer->MonsterWhisper(str,pPlayer->GetGUID(),true);
                    }
                    else
                    {
                        sprintf(str,"Dein Inventar ist voll oder du hast dieses Item bereits!!");
                        pPlayer->MonsterWhisper(str,pPlayer->GetGUID(),true);
                    }

                }
            }
            pPlayer->PlayerTalkClass->ClearMenus();
            OnGossipHello(pPlayer, pCreature);
        }

        bool OnGossipHello(Player* pPlayer, Creature* pCreature)
        {
            pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "Wieviele RecoveryPoints habe ich?", GOSSIP_SENDER_MAIN, 1000);
            pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "Recovery", GOSSIP_SENDER_MAIN, 2000);
            

            pPlayer->PlayerTalkClass->SendGossipMenu(907, pCreature->GetGUID());

            return true;
        }

        bool OnGossipSelect(Player* pPlayer, Creature* pCreature, uint32 , uint32 uiAction)
        {
            pPlayer->PlayerTalkClass->ClearMenus();
            
            switch (uiAction)
            {
            case 1000:
                RewardItem(pPlayer, pCreature, 0, 0, 0);
                break;
            case 2000:
                pPlayer->PlayerTalkClass->ClearMenus();
                pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_DOT, "Willst du das Recoveryangebot fuer diesen Charakter aktivieren? Du erhaelst Embleme zum Kaufen eines Startgears", GOSSIP_SENDER_MAIN, 2001);
                pPlayer->PlayerTalkClass->SendGossipMenu(907, pCreature->GetGUID());
                return true;
                break;
            case 2001:
                RewardItem(pPlayer, pCreature,45624,360,1);
                RewardItem(pPlayer, pCreature,40752,150,1);
                OnGossipHello(pPlayer, pCreature);
                break;
            }


            return true;
        }

};

void AddSC_recovery()
{
    new recovery();
}

